﻿@inject IJSRuntime JS
@inject ISettingService _settingService
@inject IDialogBoxService _dialogBoxService
@inject ILive2DService _live2DService
@inject IEventService _eventService
@implements IDisposable

<div class="@($"kanban-live2d {(isReady?"":"d-none")}")" id="kanban-live2d" style="@_settingService.Setting.Kanban.GetStyles()">
    <CnGalWebSite.Kanban.Components.MainCard />
    <CnGalWebSite.Kanban.Components.Buttons.ButtonGroupCard ChildContent="@ChildContent" />
    <CnGalWebSite.Kanban.Components.Dialogs.DialogBoxCard/>
</div>

@code {
    [Parameter]
    public RenderFragment ChildContent { get; set; }


    Task _task;
    CancellationTokenSource tokenSource;

    bool isReady;

    protected override void OnInitialized()
    {
        _settingService.OnSettingChanged -= OnSettingChanged;
        _settingService.OnSettingChanged += OnSettingChanged;

        _live2DService.Live2DInitialized -=OnLive2DInitialized;
        _live2DService.Live2DInitialized +=OnLive2DInitialized;
    }

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {

            await _live2DService.InitAsync();
            StateHasChanged();

            tokenSource = new CancellationTokenSource();
            _task = Task.Factory.StartNew(async (obj) =>
            {
                CancellationToken ct = (CancellationToken)obj;
                await _eventService.RunAsync(ct);

            }, tokenSource.Token);

        }
    }

    public void OnSettingChanged()
    {
        StateHasChanged();
    }

    public void OnLive2DInitialized()
    {
        isReady = true;
        StateHasChanged();
    }

    public void Dispose()
    {
        tokenSource?.Cancel();
        tokenSource?.Dispose();
        _settingService.OnSettingChanged -= OnSettingChanged;
        _live2DService.ReleaseLive2D();
        _live2DService.Live2DInitialized -= OnLive2DInitialized;
        GC.SuppressFinalize(this);
    }
}
